package cn.com.jslh.boarpro.main.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.template.TempNet;
import cn.com.jslh.boarpro.util.template.TemplateHelper;

/**
 * Created by Administrator on 2017/11/23.
 */

public class SelectDialog extends Dialog {

    private RecyclerView recyclerView;
    private SlimAdapterEx adpater;
    private SelectChoseListener selectChoseListener;
    private String ActionName;
    private EditText searchText;
    private ImageView search_img;

    public EditText getSearchText() {
        return searchText;
    }

    public void setSearchText(EditText searchText) {
        this.searchText = searchText;
    }

    public ImageView getSearch_img() {
        return search_img;
    }

    public void setSearch_img(ImageView search_img) {
        this.search_img = search_img;
        search_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msgStr = searchText.getText().toString();
                new TempNet.Builder(v.getContext())
                        .addParam("searchCont",searchText.getText().toString())
                        .setActionName(ActionName)
                        .setNetLitener(new WebService.NetSuccessListener() {
                            @Override
                            public void Success(String s) throws JSONException {
                                JSONObject jsonObject = new JSONObject(s);
                                if (jsonObject.getInt("flag") == 1) {
                                    setDatas(JSONUtil.JsonArray2List(jsonObject.getJSONArray("data")));
                                } else {
                                    setDatas(new ArrayList<JSONObject>());
                                }
                            }
                        }).build().doAction();
            }
        });
    }

    public void setBase(String actionName) {
        this.ActionName = actionName;
    }

    public SelectDialog(@NonNull Context context) {
        super(context);
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }


    public void setItemClickListener(SelectChoseListener selectChoseListener) {
        this.selectChoseListener = selectChoseListener;
    }

    public interface SelectChoseListener {
        void selectItem(String id, String title);
    }


    public void setDatas(List<JSONObject> datas) {
        if (adpater == null) {
            adpater = SlimAdapter.create(SlimAdapterEx.class)
                    .enableDiff()
                    .enableEmptyView(getContext(), R.layout.empty_layout)
                    .register(R.layout.select_item_layout, new SlimInjector<JSONObject>() {
                        @Override
                        public void onInject(final JSONObject data, IViewInjector injector) {
                            try {
                                String value = data.getString("name");
                                injector.text(R.id.title, value)
                                        .clicked(R.id.layout, new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                if (selectChoseListener != null) {
                                                    try {
                                                        selectChoseListener.selectItem(data.getString("id"), data.getString("name"));
                                                        dismiss();
                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        });
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }).attachTo(recyclerView);
        }
        adpater.updateData(datas).notifyDataSetChanged();
    }

    public static class Builder {
        private Context context;
        private RecyclerView recyclerView;
        private EditText searchText;
        private ImageView search_img;

        public Builder(Context context) {
            this.context = context;
        }

        public SelectDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final SelectDialog dialog = new SelectDialog(context);
            View view = inflater.inflate(R.layout.select_dialog_layout, null);
            recyclerView = (RecyclerView) view.findViewById(R.id.recycleview);
            ImageView back = (ImageView) view.findViewById(R.id.back);
            searchText = (EditText) view.findViewById(R.id.searchText);
            search_img = (ImageView) view.findViewById(R.id.search_img);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
            dialog.setContentView(view);
            dialog.setRecyclerView(recyclerView);
            dialog.setSearch_img(search_img);
            dialog.setSearchText(searchText);
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            return dialog;
        }
    }
}
